package com.itheima.bakery.demos.web.controller;



import com.itheima.bakery.demos.web.model.Product;
import com.itheima.bakery.demos.web.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/api/products")
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping
    public List<Product> listProducts() {
        return productService.findAllProducts();
    }

    @PostMapping
    public Product createProduct(@RequestBody Product product) {
        return productService.saveProduct(product);
    }

    @GetMapping("/{id}")
    public ResponseEntity<?> getProductById(@PathVariable Integer id) {
        Optional<Product> productOptional = productService.findProductById(id);
        if (productOptional.isPresent()) {
            return ResponseEntity.ok(productOptional.get());
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteProduct(@PathVariable Integer id) {
        boolean success = productService.deleteProduct(id);
        if (success) {
            Map<String, Object> response = new HashMap<>();
            response.put("message", "Product deleted successfully");
            response.put("productId", id);
            return ResponseEntity.ok(response);
        } else {
            Map<String, Object> response = new HashMap<>();
            response.put("error", "Product not found");
            response.put("productId", id);
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }
    @PutMapping("/{id}")
    public ResponseEntity<Product> updateProduct(@PathVariable Integer id, @RequestBody Product productRequest) {
        Optional<Product> productOptional = productService.findProductById(id);

        if (productOptional.isPresent()) {
            Product product = productOptional.get();
            product.setName(productRequest.getName() != null ? productRequest.getName() : product.getName());
            product.setDescription(productRequest.getDescription() != null ? productRequest.getDescription() : product.getDescription());
            product.setPrice(productRequest.getPrice() != null ? productRequest.getPrice() : product.getPrice());
            product.setCategoryID(productRequest.getCategoryID() != null ? productRequest.getCategoryID() : product.getCategoryID());
            product.setImageURL(productRequest.getImageURL() != null ? productRequest.getImageURL() : product.getImageURL());
            product.setDetailUrl(productRequest.getDetailUrl() != null ? productRequest.getDetailUrl() : product.getDetailUrl());
            // 如果Product类中还有其他可更新的字段，在这里添加

            return ResponseEntity.ok(productService.saveProduct(product));
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/imageUrl/{productId}")
    public String getProductImageUrl(@PathVariable Integer productId) {
        Product product = productService.getProductById(productId);
        return product.getImageURL();
    }
    @GetMapping("/search")
    public ResponseEntity<List<Product>> searchProducts(@RequestParam String query) {
        List<Product> products = productService.searchProductsByDescriptionContainingIgnoreCase(query);
        return ResponseEntity.ok(products);
    }
}
